# -*- encoding: utf-8 -*-
"""
File Name: delete_all_zabbix_container
Description:
Author : 'lirui'
date: 2024/3/4
Contact: marjey_lee@163.com
Change Activity:
2024/3/4:
"""
import ll
from ll.modules.network.ssh_util.ssh_client import SSHClient
from modules.blk.cmdb.cmdb_service import CMDBService


def _load_cmdb_container_info():
    data = CMDBService().select('zabbix_docker_info')
    return data['data']['info']


def _get_vm_ssh_clients():
    m = {}
    for ip, login_info in zabbix_login_info.items():
        c = SSHClient(
            ip=ip,
            username=login_info['username'],
            password=login_info['password']
        )
        m[ip] = c
    return m


def _close_ssh_connection(clients):
    for ip, c in clients.items():
        c.close()


def _delete_container(cmdb_info, ssh_clients_mapping):
    """

    Args:
        cmdb_info:
        ssh_clients_mapping:

    Returns:

    """
    for info in cmdb_info:
        ip = info['docker_server_ip']
        zabbix_id = info['zabbix_id']
        container_name = f'zabbix-{zabbix_id}'
        ssh = ssh_clients_mapping[ip]
        c = f'docker stop {container_name}'
        res = ssh.run(c)
        ll.get_logger().info(res)
        c = f'docker rm {container_name}'
        res = ssh.run(c)
        ll.get_logger().info(res)


def _main():
    cmdb_info = _load_cmdb_container_info()
    clients_mapping = _get_vm_ssh_clients()
    _delete_container(cmdb_info, clients_mapping)
    _close_ssh_connection(clients_mapping)


if __name__ == '__main__':
    _main()
